package com.zwxbc.funplay.servlet;

import com.zwxbc.funplay.entity.Admin;
import com.zwxbc.funplay.entity.Developer;
import com.zwxbc.funplay.entity.Player;
import com.zwxbc.funplay.service.AdminService;
import com.zwxbc.funplay.service.DeveloperService;
import com.zwxbc.funplay.service.PlayerService;
import com.zwxbc.funplay.service.impl.AdminServiceImpl;
import com.zwxbc.funplay.service.impl.DeveloperServiceImpl;
import com.zwxbc.funplay.service.impl.PlayerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			request.setCharacterEncoding("UTF-8");
			System.out.println("进入注册");
			String identity = request.getParameter("identity");
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			String name = request.getParameter("name");
			String contact = request.getParameter("contact");
			String email = request.getParameter("email");

			AdminService adminService = new AdminServiceImpl();
			PlayerService playerService = new PlayerServiceImpl();
			DeveloperService developerService = new DeveloperServiceImpl();
			// 判断用户名、联系方式、邮箱是否已存在
			if (adminService.isAdminExists(username, contact, email) || developerService.isDeveloperExists(username, contact, email) || playerService.isPlayerExists(username, contact, email)) {
				response.setContentType("application/json");
				response.setCharacterEncoding("UTF-8");
				response.getWriter().write("{\"error\": \"用户名、联系方式或邮箱已存在\"}");
				return;
			}

			switch (identity) {
				case "admin":
					Admin admin = new Admin();
					admin.setAdminUsername(username);
					admin.setAdminPassword(password);
					admin.setAdminName(name);
					admin.setAdminEmail(email);
					admin.setAdminContact(contact);
					adminService.addAdmin(admin);
					handleRegisterResponse(response);
					break;
				case "player":
					Player player = new Player();
					player.setPlayerUsername(username);
					player.setPlayerPassword(password);
					player.setPlayerName(name);
					player.setPlayerEmail(email);
					player.setPlayerContact(contact);
					player.setPlayerLevel(0L);
					player.setPlayerImage("user.jpg");
					playerService.addPlayer(player);
					handleRegisterResponse(response);
					break;
				case "developer":
					Developer developer = new Developer();
					developer.setDeveloperUsername(username);
					developer.setDeveloperPassword(password);
					developer.setDeveloperName(name);
					developer.setDeveloperContact(contact);
					developer.setDeveloperEmail(email);
					developerService.addDeveloper(developer);
					handleRegisterResponse(response);
					break;
				default:
					response.setContentType("application/json");
					response.setCharacterEncoding("UTF-8");
					response.getWriter().write("{\"error\": \"未选择身份\"}");
					break;
			}
		} catch (Exception e) {
			e.printStackTrace();
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "注册失败");
		}
	}

	private void handleRegisterResponse(HttpServletResponse response) throws IOException {
		// 注册成功
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write("{\"success\": true}");
	}
}
